﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace FacebookApp
{
    public partial class Admins_Contact : System.Web.UI.Page
    {

        private static bool Insert;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            try
            {
                grdContact.DataSource = ContactService.Contact_GetbyAll();
                grdContact.DataBind();
            }
            catch { }
        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridItem item = default(DataGridItem);
                for (int i = 0; i < grdContact.Items.Count; i++)
                {
                    item = grdContact.Items[i];
                    if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                    {
                        if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                        {
                            var obj = new ContactInfo();
                            obj.Id = item.Cells[1].Text;
                            try
                            {
                                ContactService.Contact_Delete(obj.Id);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                grdContact.CurrentPageIndex = 0;
                BindGrid();
            }
            catch
            {
            }
        }

        protected void grdContact_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            var strCA = new ContactInfo();
            strCA.Id = e.CommandArgument.ToString();
            if (e.CommandName == "Edit")
            {
                Contact dt = ContactService.Contact_GetbyId(strCA.Id);
                txtID.Text = dt.Id.ToString();
                txtName.Text = dt.Name;
                txtCompany.Text = dt.Company;
                txtAddress.Text = dt.Address;
                txtPhone.Text = dt.Phone;
                txtMail.Text = dt.Mail;
                FCKeditor1.Value = dt.Detail;
                //txtDate.Text = DateTimeClass.ConvertDate(dt.Date.ToString(), "MM/dd/yyyy");
                ddlStatus.Text = dt.Status.ToString();
                pnUpdate.Visible = true;
                pnShow.Visible = false;
            }
            else
            {
                ContactService.Contact_Delete(strCA.Id);
                BindGrid();
            }
        }
        public string Status(string Values)
        {
            if (Values == "False")
                return "Chưa xem";
            return "Đã xem";
        }
        protected void lblBack_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            Insert = false;
        }

        protected void lblUpdate_Click(object sender, EventArgs e)
        {
            #region[TestInput]

            if (txtName.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Họ tên không thể trống !");
                txtName.Focus();
                return;
            }
            if (txtAddress.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Địa chỉ không thể trống !");
                txtAddress.Focus();
                return;
            }
            if (txtPhone.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Điện thoại không thể trống !");
                txtPhone.Focus();
                return;
            }
            if (txtMail.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Mail không thể trống !");
                txtMail.Focus();
                return;
            }
            if (FCKeditor1.Value.Trim().Equals(""))
            {
                WebMsgBox.Show("Nội dung không thể trống !");
                FCKeditor1.Focus();
                return;
            }

            #endregion

            var obj = new ContactInfo();
            obj.Id = txtID.Text;
            obj.Name = txtName.Text;
            obj.Company = txtCompany.Text;
            obj.Address = txtAddress.Text;
            obj.Phone = txtPhone.Text;
            obj.Mail = txtMail.Text;
            obj.Detail = FCKeditor1.Value;
            //obj.Date = DateTime.Now.ToString();
            obj.Status = ddlStatus.SelectedValue;
            ContactService.Contact_Update(obj);
            BindGrid();
            pnShow.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }

        protected void grdContact_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdContact.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void grdContact_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdContact.ClientID + "_row" + e.Item.ItemIndex;
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut",
                                              "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")");
                        ((CheckBox)checkBox).Attributes.Add("onClick",
                                                             "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex +
                                                             ")");
                    }
                }
            }
        }
    }
}